//
//  FriendTableViewCell.m
//  BaChat
//
//  Created by 刘洋 on 16/6/15.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "FriendTableViewCell.h"

@interface FriendTableViewCell ()


@property(nonatomic,retain)BaseLabel *nameLabel;
@property(nonatomic,retain)BaseLabel *messageLabel;
@property(nonatomic,retain)BaseImageView *iconImage;
@property(nonatomic,retain)BaseButton *agreeButton;
@property(nonatomic,retain)BaseButton *refuseButton;
@property(nonatomic,retain)UIView *lineView;



@end



@implementation FriendTableViewCell


-(void)dealloc{
    [_lineView release];
    [_nameLabel release];
    [_messageLabel release];
    [_iconImage release];
    [_friendModel release];
    [super dealloc];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
    }
    return self;
    
    
}

-(void)createSubviews{
    
    self.iconImage = [[BaseImageView alloc]init];
    self.iconImage.backgroundColor = [UIColor blueColor];
    [self.contentView addSubview:self.iconImage];
    [_iconImage release];
    
    self.nameLabel = [[BaseLabel alloc]init];
    self.nameLabel.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.nameLabel];
    [_nameLabel release];
    
    self.messageLabel = [[BaseLabel alloc]init];
    self.messageLabel.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.messageLabel];
    self.messageLabel.font = [UIFont systemFontOfSize:13];
    self.messageLabel.textColor = [UIColor blackColor];
    self.messageLabel.alpha = 0.5;
    [_messageLabel release];
    
    
    self.agreeButton = [BaseButton buttonWithType:UIButtonTypeCustom];
    [self.agreeButton setTitle:@"同意" forState:UIControlStateNormal];
    self.agreeButton.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:self.agreeButton];
    [self.agreeButton addTarget:self action:@selector(agreeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.agreeButton.layer.masksToBounds = YES;
    self.agreeButton.layer.cornerRadius = 3;
    
    
    
    self.refuseButton = [BaseButton buttonWithType:UIButtonTypeCustom];
    [self.refuseButton setTitle:@"拒绝" forState:UIControlStateNormal];
    self.refuseButton.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:self.refuseButton];
    [self.refuseButton addTarget:self action:@selector(refuseButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.refuseButton.layer.masksToBounds = YES;
    self.refuseButton.layer.cornerRadius = 3;
    
    
    self.lineView = [[UIView alloc]init];
    self.lineView.backgroundColor = RGBAColor(220, 220, 220);
    [self.contentView addSubview:self.lineView];
    [_lineView release];
    
    
}


-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.iconImage.frame = CGRectMake(10, 5, 40, 40);
    
    self.nameLabel.frame = CGRectMake(self.iconImage.frame.origin.x + self.iconImage.frame.size.width + 10, 5, ROOTWIDTH / 2, 20);
    
    self.messageLabel.frame = CGRectMake(self.nameLabel.frame.origin.x, self.nameLabel.frame.origin.y + self.nameLabel.frame.size.height, ROOTWIDTH / 2, 20);
   
 
    
    self.refuseButton.frame = CGRectMake(ROOTWIDTH - ROOTWIDTH / 5 - 10, self.contentView.frame.size.height / 2 - 15,  ROOTWIDTH / 5 , 30);
   self.agreeButton.frame = CGRectMake(ROOTWIDTH - ROOTWIDTH / 5 * 2 - 20 , self.refuseButton.frame.origin.y, ROOTWIDTH / 5, 30);
    
    self.lineView.frame = CGRectMake(10, 50, ROOTWIDTH - 20, 1);
    
    
}


-(void)setFriendModel:(NewFriendModel *)friendModel{
    if (_friendModel != friendModel) {
        [_friendModel release];
        _friendModel = [friendModel retain];
    }
    self.nameLabel.text = _friendModel.name;
    self.messageLabel.text = _friendModel.message;
    
    
    
}


-(void)agreeButtonAction:(BaseButton *)button{
    //loginInfo字典,存当前用户的信息
    
      NSString *loginUsername = [[EaseMob sharedInstance].chatManager loginInfo][@"username"];
    NSLog(@"%@",loginUsername);
    
    EMError *error = nil;
    [[EaseMob sharedInstance].chatManager acceptBuddyRequest:self.friendModel.name error:&error];
    if (!error) {
        NSLog(@"同意好友申请");
        
        [button setTitle:@"已同意" forState:UIControlStateNormal];
        [[AppDelegate shareFriendDic]removeObjectForKey:self.friendModel.name];
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"contactsBadgeValue" object:[NSString stringWithFormat:@"%ld",(unsigned long)[AppDelegate shareFriendDic].count]];
        
    }
    

    
}


-(void)refuseButtonAction:(BaseButton *)button{
    
    EMError *error = nil;
   [[EaseMob sharedInstance].chatManager rejectBuddyRequest:self.friendModel.name reason:@"我不认识你" error:&error];
    if (!error) {
        NSLog(@"发送拒绝成功");
        
        [button setTitle:@"已拒绝" forState:UIControlStateNormal];
        
        [[AppDelegate shareFriendDic]removeObjectForKey:self.friendModel.name];
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"contactsBadgeValue" object:[NSString stringWithFormat:@"%ld",(unsigned long)[AppDelegate shareFriendDic].count]];
        
    }
    
    
    
    
}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
